# -*- coding:utf-8 -*-

## =====================================================================
## Copyright (C) 2007-2011  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## =====================================================================


from reportlab.lib import colors
from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table
from reportlab.lib.styles import getSampleStyleSheet
import time


class StayPDF():
	def __init__(self, path=None, log=None, stay=None, products=None, services=None, parkings=None, residents=None, bills=None):
		self.path=path
		self.log=log
		self.stay=stay
		self.products=products
		self.services=services
		self.parkings=parkings
		self.residents=residents
		self.bills=bills
		self.check=0
		self.log.info("\tStayPDF\tStart")
		self.pdfPage()
	
	def pdfPage(self):
		# Container for 'Flowable' objects
		elements = []
		
		# Collection of style sheets pre-made for us
		styles = getSampleStyleSheet()
		
		# Basic document for us to write to 'SortReference_datetime.pdf'
		dt=time.strftime("%Y%m%d%H%M" ,time.localtime())
		doc = SimpleDocTemplate("pdf/Voucher"+self.stay.voucher+"_"+dt+".pdf")
		
		# Header Stay
		headerStay=Paragraph("Confirmation de réservation séjour N°: "+self.stay.voucher, styles["Title"])
		elements.append(headerStay)
		
		if self.stay.status == u"O":
			status="en option"
		elif self.stay.status == u"R":
			status="reservé"
		if self.stay.status == u"F":
			status="réservation ferme"
		if self.stay.status == u"A":
			status="annulée"
			
		# Details Stay
		detailStay=[	["Date d'arrivée :", self.stay.checkIn.strftime("%d/%m/%Y - %Hh%M") ],
								["Date de départ :", self.stay.checkOut.strftime("%d/%m/%Y - %Hh%M") ],
								["Réservation : ", status],
								["Code remise : ", str(self.stay.discount.code)],
								["Remarque :", self.stay.notes]
							]
		
		tableDetailStay=Table(detailStay)
		## elements.append(tableDetailStay)
		
		# Contact coordinate
		customer=[	["Contact"],
							[str(self.stay.contact.fname)+" "+str(self.stay.contact.lname)],
							[str(self.stay.contact.address1)],
							[str(self.stay.contact.city.zipCode.code)+" - "+str(self.stay.contact.city.name)],
							["Tel: "+str(self.stay.contact.tel)] ]
							
		tableCustomer=Table(customer)
		## elements.append(tableCustomer)
		
		theStay=[ [tableDetailStay, tableCustomer] ]
		tableTheStay=Table(theStay)
		elements.append(tableTheStay)
		
		# Products linked
		if len(list(self.products)) > 0:
			productsLinked=[ ["Produits rattacher au séjour", "Réference", "Résidence", "Ville", "Code Postal"] ]
			for p in self.products:
				productsLinked.append( [p.wish.designation, p.wish.reference.code, p.wish.place.company, p.wish.place.city.name, p.wish.place.city.zipCode.code ] )
			tableProductsLinked=Table(productsLinked)
			elements.append(tableProductsLinked)
		else:
			noProduct=Paragraph("Aucun produit rattaché à ce séjour...", styles["Normal"])
			elements.append(noProduct)
		
		# Services linked
		if len(list(self.services)) > 0:
			servicesLinked=[ ["Services rattacher au séjour", "Réference", "Résidence", "Ville", "Code Postal"] ]
			for s in self.services:
				servicesLinked.append( [s.wish.designation, s.wish.reference.code, s.wish.place.company, s.wish.place.city.name, s.wish.place.city.zipCode.code ] )
			tableServicesLinked=Table(servicesLinked)
			elements.append(tableServicesLinked)
		else:
			noService=Paragraph("Aucun service réservé avec ce séjour...", styles["Normal"])
			elements.append(noService)
		
		# Parkings linked
		if len(list(self.parkings)) > 0:
			parkingsLinked=[ ["Parkings rattacher au séjour", "Réference", "Résidence", "Ville", "Code Postal"] ]
			for pk in self.parkings:
				parkingsLinked.append( [pk.wish.description, pk.wish.reference.code, pk.wish.place.company, pk.wish.place.city.name, pk.wish.place.city.zipCode.code ] )
			tableParkingsLinked=Table(parkingsLinked)
			elements.append(tableParkingsLinked)
		else:
			noParking=Paragraph("Aucun parking réservé avec ce séjour...", styles["Normal"])
			elements.append(noParking)
		
		# Residents linked
		if len(list(self.residents)) > 0:
			residentsLinked=[ ["Résident enregistré sur ce séjour", "Age", "Ville", "Code Postal"] ]
			for r in self.residents:
				residentsLinked.append( [r.person.fname+" "+r.person.lname, r.old, r.person.city.name, r.person.city.zipCode.code ] )
			tableResidentsLinked=Table(residentsLinked)
			elements.append(tableResidentsLinked)
		else:
			noResident=Paragraph("Aucun résident enregistré sur ce séjour...", styles["Normal"])
			elements.append(noResident)
		
		# Bill linked
		if len(list(self.bills)) > 0:
			billsLinked=[ ["Pièce de vente rattachée à ce séjour", "Montant", "Etat", "Client", "Code Postal"] ]
			for b in self.bills:
				billsLinked.append( [b.bill.sort+b.bill.reference+" Date : "+b.bill.date.strftime("%d/%m/%Y"), "%.2f"%b.bill.tTCPrice, b.bill.status, b.bill.customer.fname+" "+b.bill.customer.lname, b.bill.customer.city.zipCode.code ])
			tableBillsLinked=Table(billsLinked)
			elements.append(tableBillsLinked)
		else:
			noBill=Paragraph("Aucune pièce de vente rattachée à ce séjour...", styles["Normal"])
			elements.append(noBill)
		
		# write the document to disk
		doc.build(elements)
		
		self.check=1
	